//
//  HCCallListModel.m
//  HelloCall
//
//  Created by WangYongpeng on 13-9-21.
//  Copyright (c) 2013年 HC. All rights reserved.
//

#import "HCCallListModel.h"
#import "HCSuperCRMModel.h"
#import "HCNetworkManger.h"




@implementation HCCallInfo

@synthesize ticketID = m_ticketID;
@synthesize phoneNumber = m_phoneNumber;
@synthesize callInfoType = m_callInfoType;
@synthesize callSourceType = m_callSourceType;
@synthesize callSource = m_callSource;
@synthesize initTime = m_initTime;
@synthesize callNumber = m_callNumber;
@synthesize sumCallTime = m_sumCallTime;
@synthesize visitDelay = m_visitDelay;
//@synthesize remarkInfoList = m_remarkInfoList;


- (void)dealloc
{
    self.phoneNumber = nil;
    self.initTime = nil;
    self.visitDelay = nil;
//    self.remarkInfoList = nil;
    self.callSource = nil;
    self.ticketID = nil;
    
    [super dealloc];
}

- (void)copy:(HCCallInfo*)info
{
    self.ticketID = info.ticketID;
    self.phoneNumber = info.phoneNumber;
    self.callInfoType = info.callInfoType;
    self.callSourceType = info.callSourceType;
    self.callSource = info.callSource;
    self.initTime = info.initTime;
    self.callNumber = info.callNumber;
    self.sumCallTime = info.sumCallTime;
    self.visitDelay = info.visitDelay;
//    self.remarkInfoList = info.remarkInfoList;
}

@end

@interface HCCallListModel ()<HCNetworkObserver>

@property (nonatomic, retain) NSMutableArray* untreatedCallInfoArray;
@property (nonatomic, retain) NSMutableArray* giveUpCallInfoArray;
@property (nonatomic, retain) NSMutableArray* shiftCallInfoArray;

@end

@implementation HCCallListModel

@synthesize selfPhoneNumber = m_selfPhoneNumber;
@synthesize untreatedCallInfoArray = m_untreatedCallInfoArray;
@synthesize giveUpCallInfoArray = m_giveUpCallInfoArray;
@synthesize shiftCallInfoArray = m_shiftCallInfoArray;
@synthesize numCount = m_numCount; //号码数量
@synthesize tgNumCount = m_tgNumCount; //推广来源
@synthesize assignNumCount = m_assignNumCount; //企业分配数
@synthesize callbackNumCount = m_callbackNumCount; //回访总数
//@synthesize delegate = m_delegate;

static HCCallListModel* g_defaultModel = nil;

+ (HCCallListModel*)defaultModel
{
    if (nil == g_defaultModel)
    {
        g_defaultModel = [[HCCallListModel alloc] init];
    }
    
    return g_defaultModel;
}

- (id)init
{
    self = [super init];
    if (nil != self)
    {        
        self.selfPhoneNumber = @"13110991099";
        
        self.untreatedCallInfoArray = [NSMutableArray arrayWithCapacity:0];
        self.giveUpCallInfoArray = [NSMutableArray arrayWithCapacity:0];
        self.shiftCallInfoArray = [NSMutableArray arrayWithCapacity:0];
        
        [[HCNetworkManger shareManger] addObserver:self];
        
        
//        for (CallInfoType i = CallInfoType_untreated; i < CallInfoType_superCRM; i++)
//        {
//            [[HCNetworkManger shareManger] getQueryTicketListByTicketType:i offset:0];
//        }
        
        
        //测试代码
//        HCCallInfo* info = [[[HCCallInfo alloc] init] autorelease];
//        info.phoneNumber = @"123456789";
//        info.callSourceType = CallSourceType_allocation;
//        info.ID = 1;
//        
//        HCCallRemarkInfo* remarkInfo = [[[HCCallRemarkInfo alloc] init] autorelease];
//        remarkInfo.addRemarkDate = [NSDate date];
//        remarkInfo.mobliePhoneNumber = @"123123";
//        NSMutableArray* remarkInfoArray = [NSMutableArray arrayWithCapacity:3];
//        [remarkInfoArray addObject:remarkInfo];
//        
//        remarkInfo = [[[HCCallRemarkInfo alloc] init] autorelease];
//        remarkInfo.addRemarkDate = [NSDate date];
//        remarkInfo.mobliePhoneNumber = @"3456";
//        [remarkInfoArray addObject:remarkInfo];
//        
//        remarkInfo = [[[HCCallRemarkInfo alloc] init] autorelease];
//        remarkInfo.addRemarkDate = [NSDate date];
//        remarkInfo.mobliePhoneNumber = @"7896";
//        [remarkInfoArray addObject:remarkInfo];
//        
//        info.remarkInfoList = remarkInfoArray;
//        [self.untreatedCallInfoArray addObject:info];
//        
//        info = [[[HCCallInfo alloc] init] autorelease];
//        info.phoneNumber = @"23456789";
//        info.ID = 2;
//        info.callSourceType = CallSourceType_allocation;
//        [self.untreatedCallInfoArray addObject:info];
//        
//        
//        info = [[[HCCallInfo alloc] init] autorelease];
//        info.phoneNumber = @"3456789";
//        info.ID = 3;
//        info.callSourceType = CallSourceType_allocation;
//        [self.untreatedCallInfoArray addObject:info];
//        
//        info = [[[HCCallInfo alloc] init] autorelease];
//        info.phoneNumber = @"456789";
//        info.ID = 4;
//        info.callSourceType = CallSourceType_allocation;
//        [self.untreatedCallInfoArray addObject:info];
//        
//        info = [[[HCCallInfo alloc] init] autorelease];
//        info.phoneNumber = @"56789";
//        info.ID = 5;
//        info.callSourceType = CallSourceType_allocation;
//        [self.untreatedCallInfoArray addObject:info];
//        
//        info = [[[HCCallInfo alloc] init] autorelease];
//        info.phoneNumber = @"6789";
//        info.ID = 6;
//        info.callSourceType = CallSourceType_allocation;
//        [self.untreatedCallInfoArray addObject:info];
//        
//        
//        info = [[[HCCallInfo alloc] init] autorelease];
//        info.phoneNumber = @"456789";
//        info.ID = 7;
//        info.callSourceType = CallSourceType_allocation;
//        [self.giveUpCallInfoArray addObject:info];
//        
//        info = [[[HCCallInfo alloc] init] autorelease];
//        info.phoneNumber = @"56789";
//        info.ID = 8;
//        info.callSourceType = CallSourceType_allocation;
//        [self.giveUpCallInfoArray addObject:info];
//        
//        info = [[[HCCallInfo alloc] init] autorelease];
//        info.phoneNumber = @"6789";
//        info.ID = 9;
//        info.callSourceType = CallSourceType_allocation;
//        [self.giveUpCallInfoArray addObject:info];
//        
//        
//        info = [[[HCCallInfo alloc] init] autorelease];
//        info.phoneNumber = @"123456789";
//        info.ID = 10;
//        info.callSourceType = CallSourceType_allocation;
//        [self.shiftCallInfoArray addObject:info];
//        
//        info = [[[HCCallInfo alloc] init] autorelease];
//        info.phoneNumber = @"23456789";
//        info.ID = 11;
//        info.callSourceType = CallSourceType_allocation;
//        [self.shiftCallInfoArray addObject:info];
//        
//        info = [[[HCCallInfo alloc] init] autorelease];
//        info.phoneNumber = @"3456789";
//        info.ID = 12;
//        info.callSourceType = CallSourceType_allocation;
//        [self.shiftCallInfoArray addObject:info];
    }
    return self;
}

- (void)dealloc
{
    g_defaultModel = nil;
    [[HCNetworkManger shareManger] removeObserver:self];
    
    self.untreatedCallInfoArray = nil;
    self.giveUpCallInfoArray = nil;
    self.shiftCallInfoArray = nil;
    
    [super dealloc];
}

- (void)reset
{
    self.untreatedCallInfoArray = nil;
    self.giveUpCallInfoArray = nil;
    self.shiftCallInfoArray = nil;
    
    self.untreatedCallInfoArray = [NSMutableArray arrayWithCapacity:0];
    self.giveUpCallInfoArray = [NSMutableArray arrayWithCapacity:0];
    self.shiftCallInfoArray = [NSMutableArray arrayWithCapacity:0];
}

- (void)changeCallInfoTypeByID:(int)id callInfoType:(CallInfoType)type
{
    HCCallInfo* callInfo = nil;
    NSMutableArray* tempArray = nil;
    
    for (HCCallInfo* info in self.untreatedCallInfoArray)
    {
        if (info.ID == id)
        {
            callInfo = [info retain];
            tempArray = self.untreatedCallInfoArray;
            break;
        }
    }
    
    if (nil == callInfo)
    {
        for (HCCallInfo* info in self.giveUpCallInfoArray)
        {
            if (info.ID == id)
            {
                callInfo = [info retain];
                tempArray = self.giveUpCallInfoArray;
                break;
            }
        }
    }
    
    if (nil == callInfo)
    {
        for (HCCallInfo* info in self.shiftCallInfoArray)
        {
            if (info.ID == id)
            {
                callInfo = [info retain];
                tempArray = self.shiftCallInfoArray;
                break;
            }
        }
    }
    
    if (nil != callInfo)
    {
        switch(type)
        {
            case CallInfoType_giveUp:
            {
                [self.giveUpCallInfoArray addObject:callInfo];
                break;
            }
            case CallInfoType_shift:
            {
                [self.shiftCallInfoArray addObject:callInfo];
                break;
            }
            case CallInfoType_superCRM:
            {
                [[HCSuperCRMModel defaultModel] addCustonInfoByCallInfo:callInfo];
            }
            default:
            {
                break;
            }
        }
    }
    
    [tempArray removeObject:callInfo];
}

- (void)addCallInfoByCustomInfo:(HCCRMCustomInfo*)customInfo callInfoType:(CallInfoType)type
{
    HCCallInfo* info = [[[HCCallInfo alloc] init] autorelease];
    info.phoneNumber = customInfo.customPhoneNumber;
//    info.ticketID = customInfo.;
    info.callSourceType = customInfo.sourceType;
    info.callInfoType = type;
    info.initTime = customInfo.initTime;
    info.callNumber = customInfo.visitCount;
    info.sumCallTime = customInfo.sumCallTime;
    info.visitDelay = nil;
//    info.remarkInfoList = customInfo.remarkInfoList;
    switch (type)
    {
        case CallInfoType_giveUp:
        {
            [self.giveUpCallInfoArray addObject:info];
            break;
        }
        case CallInfoType_shift:
        {
            [self.shiftCallInfoArray addObject:info];
            break;
        }
        default:
            break;
    }
    
}

- (NSArray*)getCallInfoArrayWithType:(CallInfoType)type
{
    switch (type)
    {
        case CallInfoType_untreated:
        {
            return self.untreatedCallInfoArray;
        }
        case CallInfoType_giveUp:
        {
            return self.giveUpCallInfoArray;
        }
        case CallInfoType_shift:
        {
            return self.shiftCallInfoArray;
        }
        default:
        {
            return nil;
        }
    }
}

- (int)getCallInfoCountWithSourceType:(CallSourceType)type
{
    int result = 0;
    
    switch (type) {
        case CallSourceType_allocation:
        {
            result = self.assignNumCount;
            break;
        }
        case CallSourceType_spread:
        {
            result = self.tgNumCount;
            break;
        }
        case CallSourceType_visit:
        {
            result = self.callbackNumCount;
            break;
        }
        default:
            break;
    }
    
//    for (HCCallInfo* info in self.untreatedCallInfoArray)
//    {
//        if (info.callSourceType == type)
//        {
//            result++;
//        }
//    }
//    
//    for (HCCallInfo* info in self.giveUpCallInfoArray)
//    {
//        if (info.callSourceType == type)
//        {
//            result++;
//        }
//    }
//    
//    for (HCCallInfo* info in self.shiftCallInfoArray)
//    {
//        if (info.callSourceType == type)
//        {
//            result++;
//        }
//    }
    
    return result;
}

- (int)getAllCallInfoCount
{
    return self.untreatedCallInfoArray.count + self.giveUpCallInfoArray.count + self.shiftCallInfoArray.count;
}

- (void)addCallInfo:(HCCallInfo*)info type:(CallInfoType)type
{
    if ([self getCallInfobyUserId:info.ticketID] != nil)
    {
        return;
    }
    
    switch (type)
    {
        case CallInfoType_untreated:         //未处理
        {
            [self.untreatedCallInfoArray addObject:info];
            break;
        }
        case CallInfoType_giveUp:            //放弃
        {
            [self.giveUpCallInfoArray addObject:info];
            break;
        }
        case CallInfoType_shift:             //申请转移
        {
            [self.shiftCallInfoArray addObject:info];
            break;
        }
        default:
        {
            break;
        }
    }
}

- (BOOL)modifyCallInfo:(HCCallInfo*)info byUserId:(NSString*)userID
{
    NSMutableArray* callInfoArray = nil;
    
    for (CallInfoType i = CallInfoType_untreated; i < CallInfoType_superCRM; i++)
    {
        switch (i)
        {
            case CallInfoType_untreated:         //未处理
            {
                callInfoArray = self.untreatedCallInfoArray;
                break;
            }
            case CallInfoType_giveUp:            //放弃
            {
                callInfoArray = self.giveUpCallInfoArray;
                break;
            }
            case CallInfoType_shift:             //申请转移
            {
                callInfoArray = self.shiftCallInfoArray;
                break;
            }
            default:
            {
                break;
            }
        }
        if (callInfoArray != nil)
        {
            int index = 0;
            for (HCCallInfo* infoNode in callInfoArray)
            {
                if ([infoNode.ticketID isEqualToString:userID])
                {
                    [infoNode copy:info];
                    return YES;
                }
                index++;
            }
        }
    }
    return NO;
}

- (HCCallInfo*)getCallInfobyUserId:(NSString*)userID
{
    NSMutableArray* callInfoArray = nil;
    
    for (CallInfoType i = CallInfoType_untreated; i < CallInfoType_superCRM; i++)
    {
        switch (i)
        {
            case CallInfoType_untreated:         //未处理
            {
                callInfoArray = self.untreatedCallInfoArray;
                break;
            }
            case CallInfoType_giveUp:            //放弃
            {
                callInfoArray = self.giveUpCallInfoArray;
                break;
            }
            case CallInfoType_shift:             //申请转移
            {
                callInfoArray = self.shiftCallInfoArray;
                break;
            }
            default:
            {
                break;
            }
        }
        if (callInfoArray != nil)
        {
            for (HCCallInfo* infoNode in callInfoArray)
            {
                if ([infoNode.ticketID isEqualToString:userID])
                {
                    return infoNode;
                }
            }
        }
    }
    return nil;
}

#pragma mark -- HCNetworkObserver

- (void)notifyNetworkMsg:(int)msgID data:(void*) data;
{
    /*pause modify———————————————------
     

     pause modify———————————————————*/
 }


@end
